package com.itheima.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @author Xiao
 * @date 2021/9/13
 */
@Component //通知类必须配置成Spring管理的bean
@Aspect //设置当前类为切面类类
public class AccountAdvice {
    //设置切入点
    @Pointcut("execution(* com.itheima.*.AccountService.*(..))")
    private void pt(){}
    //@Around("pt()")
    public void countTime(ProceedingJoinPoint pjp) throws Throwable{
        //获取执行的签名对象
        Signature signature = pjp.getSignature();
        //获取接口/类全限定名
        String typeName = signature.getDeclaringTypeName();
        //获取方法名
        String methodName = signature.getName();
        //执行万次 --> 前后用时间System.currentTimeMillis()环绕
        long start = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            pjp.proceed();
        }
        long end = System.currentTimeMillis();
        long time = end - start;
        System.out.println("万次执行" + typeName + "." + methodName + "-->" + time);
    }
}
